"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MiniLayout = void 0;

var _react = _interopRequireWildcard(require("react"));

var _taro = _interopRequireDefault(require("@tarojs/taro"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var MiniLayout = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var _ref$imgs = _ref.imgs,
      imgs = _ref$imgs === void 0 ? [] : _ref$imgs,
      _ref$currentUrl = _ref.currentUrl,
      currentUrl = _ref$currentUrl === void 0 ? '' : _ref$currentUrl,
      _ref$visible = _ref.visible,
      visible = _ref$visible === void 0 ? false : _ref$visible,
      _ref$onComplete = _ref.onComplete,
      onComplete = _ref$onComplete === void 0 ? function () {} : _ref$onComplete,
      _ref$onSuccess = _ref.onSuccess,
      onSuccess = _ref$onSuccess === void 0 ? function () {} : _ref$onSuccess,
      _ref$onFail = _ref.onFail,
      onFail = _ref$onFail === void 0 ? function () {} : _ref$onFail;

  var imgsRef = _react.default.useRef([]);

  var currentImgRef = _react.default.useRef('');

  var show = (0, _react.useCallback)(function () {
    _taro.default.previewImage({
      current: currentImgRef.current,
      urls: imgsRef.current,
      complete: function complete(res) {
        onComplete(res);
      },
      success: function success(res) {
        onSuccess(res);
      },
      fail: function fail(res) {
        onFail(res);
      }
    });
  }, [imgs, visible, currentUrl]);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      show: show
    };
  }, [show]);
  (0, _react.useEffect)(function () {
    imgsRef.current = imgs;
  }, [imgs]);
  (0, _react.useEffect)(function () {
    if (visible) {
      _taro.default.previewImage({
        current: currentImgRef.current,
        urls: imgsRef.current,
        complete: function complete(res) {
          onComplete(res);
        },
        success: function success(res) {
          onSuccess(res);
        },
        fail: function fail(res) {
          onFail(res);
        }
      });
    }
  }, [visible]);
  (0, _react.useEffect)(function () {
    currentImgRef.current = currentUrl;
  }, [currentUrl]);
  return null;
});
exports.MiniLayout = MiniLayout;